# gozerbot/utils/trace.py
#
#

""" trace related functions """

__copyright__ = 'this file is in the public domain'

from log import rlog
import sys, re

# regular expression to determine thread name
methodre = re.compile('method\s+(\S+)', re.I)
funcre = re.compile('function\s+(\S+)', re.I)

def calledfrom(frame):
    try:
        plugname = frame.f_back.f_code.co_filename
        name = plugname.split('/')[-1][:-3]
        if name == '__init__':
            name = plugname.split('/')[-2]
        if name == 'generic':
            plugname = frame.f_back.f_back.f_code.co_filename
            name = plugname.split('/')[-1][:-3]
    except AttributeError, ex:
        rlog(10, 'trace', str(ex))
        name = None
    del frame
    return name

def callstack(frame):
    result = []
    loopframe = frame
    while 1:
        try:
            plugname = loopframe.f_back.f_code.co_filename
            result.append("%s:%s" % (plugname.split('.')[-1][:-3], \
loopframe.f_back.f_lineno))
            loopframe = loopframe.f_back
        except:
            break
    del frame
    return result

def whichmodule(depth=1):
    try:
        frame = sys._getframe(depth)
        plugfile = frame.f_back.f_code.co_filename[:-3].split('/')
        lineno = frame.f_back.f_lineno
        mod = []
        for i in plugfile[::-1]:
            mod.append(i)
            if i == 'gozerbot' or i == 'gozerplugs':
                break
        modstr = '.'.join(mod[::-1]) + ':' + str(lineno)
    except AttributeError:
        modstr = None
    del frame
    return modstr

def getname(func):

    """ get name of function/method. """

    name = ""

    method = re.search(methodre, str(func))
    if method:
        name = method.group(1)
    else: 
        function = re.search(funcre, str(func))
        if function:
            name = function.group(1)
        else:
            name = str(func)
    return name
